
package br.com.agenda.server.proxy;


import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;

import br.com.agenda.server.servidor.IServicoServidor;

public class ProxyMain extends UnicastRemoteObject {

	private static final long serialVersionUID = -6873639833237419108L;
	private static final String URIServer = "rmi://localhost:12345/servidor";
	private static final String URIProxy = "rmi://localhost:12346/proxy";
	protected ProxyMain() throws RemoteException {}

	public static void main(String[] args) {
		
		try{ //Proxy atuando como Servidor
			ProxyMain proxy = new ProxyMain();
			LocateRegistry.createRegistry(12346);
			IServicoServidor servicosServidor = (IServicoServidor) Naming.lookup(URIServer);
	        IDispatcher dispatcher = new Dispatcher(servicosServidor);
	        Naming.bind(URIProxy, dispatcher);
	        System.out.println("Server proxy started and up!");
	       
	    } 
		
	    catch (AlreadyBoundException ex) {
	        Logger.getLogger(ProxyMain.class.getName()).log(Level.SEVERE, null, ex);
	    } 
	    catch (MalformedURLException ex) {
	        Logger.getLogger(ProxyMain.class.getName()).log(Level.SEVERE, null, ex);
	    } 
	    catch (RemoteException ex) {
	        Logger.getLogger(ProxyMain.class.getName()).log(Level.SEVERE, null, ex);
	    } catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	  	    
	    
	}

}
